﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using System.ComponentModel;
using System.Windows.Input;

namespace TestFYP
{
    class DocumentPrep
    {
        private Microsoft.Office.Interop.Word.Application app;
        private Document doc;
        private Object Filename;
        private BackgroundWorker bw;
        bool docopened = false;
        object missing = System.Reflection.Missing.Value;
        public List<Counters> counterlist {get;set;}
        private List<CountTemplate> featurelist;
        private CountTemplate ct;
        private Window1 wnd;

        public DocumentPrep(Window1 wnd)
        {
            counterlist = new List<Counters>();
            featurelist = new List<CountTemplate>();
            this.wnd = wnd;
            bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.DoWork +=new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            
        }

        public void loadDoc(String filename) 
        {
            Filename = filename;
            app = new Microsoft.Office.Interop.Word.Application();
            doc = new Document();
            bw.RunWorkerAsync();
        }

        public void closeDoc()
        {            
            if (docopened)
            {
                doc.Close(ref missing, ref missing, ref missing);
                app.Quit(ref missing, ref missing, ref missing);
                docopened = false;
            }
      
        }

        private void bw_DoWork(Object Sender, DoWorkEventArgs e) 
        {
            doc = app.Documents.Open(ref Filename, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing,
                               ref missing, ref missing, ref missing, ref missing);

            doc.Activate();
            docopened = true;
            doc.Select();
            ChapterBreak();
            //ct = new CountTemplate(app, doc);
            foreach (Counters counter in counterlist)
            {

                Console.WriteLine(counter.range.Start + "\n"+counter.range.End+"\n\n");
                featurelist.Add(new CountTemplate(app, doc, counter));


            }
            foreach(CountTemplate feature in featurelist)
            {
                Console.WriteLine("1");
                feature.doCount();                
            }

            Console.WriteLine("2");

            foreach (Counters counter in counterlist)
            {
                //Console.WriteLine(counter.range.Text);
                //Console.WriteLine("comma: " + counter.punc_comma.ToString());
                //Console.WriteLine("period: " + counter.punc_period.ToString());
                //Console.WriteLine("question: " + counter.punc_question.ToString());

                //char ch = 'a';

                //foreach (int atoz in counter.graph_atoz)
                //{
                //    Console.WriteLine(ch.ToString() + ": " + atoz.ToString());
                //    ch++;
                //}

                //ch = 'A';

                //foreach (int AtoZ in counter.graph_AtoZ)
                //{
                //    Console.WriteLine(ch.ToString() + ": " + AtoZ.ToString());
                //    ch++;
                //}

                Console.WriteLine(counter.punc_CloseBracket);
                Console.WriteLine(counter.punc_colon);
                Console.WriteLine(counter.punc_comma);
                Console.WriteLine(counter.punc_exclamation);
                Console.WriteLine(counter.punc_invertcomma);
                Console.WriteLine(counter.punc_OpenBracket);
                Console.WriteLine(counter.punc_period);
                Console.WriteLine(counter.punc_question);
                Console.WriteLine(counter.punc_semicolon);
                Console.WriteLine("\n\n\n");
            }

            StyleChecker st = new StyleChecker(counterlist);
            st.CheckStyles();

        }

        private void bw_ProgressChanged(Object Sender, ProgressChangedEventArgs e)
        { 
        
        }

        private void bw_RunWorkerCompleted(Object Sender, RunWorkerCompletedEventArgs e)
        {
            Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
        }
   
        private void ChapterBreak()
        {
            int count = 0;
            object Rstart = 0;
            object Rend = 0;

            foreach (Paragraph paragraph in app.Selection.Paragraphs)
            {
                if (paragraph.OutlineLevel == WdOutlineLevel.wdOutlineLevel1)
                {
                    Console.WriteLine(paragraph.Range.Text);
                    String str = paragraph.Range.Text;                    
                    if (count == 0)
                    {
                        Rstart = paragraph.Range.End;
                        count++;
                    }
                    else
                    {  
                        Rend = paragraph.Range.Start;
                        if(!Rstart.Equals(Rend))
                            counterlist.Add(new Counters(doc.Range(ref Rstart, ref Rend),str));
                        Rstart = paragraph.Range.End;
                    }          

                }
            }
        }
    }
}
